<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/16
 * Time: 9:21
 */

namespace app\admin\controller;


use app\admin\model\AssembleList;
use app\admin\model\GoodsList;
use think\facade\Env;

class Assemble extends Base
{


    public function assembleList(){
        if ($this->request->isAjax()){
            $page = input('get.page');
            $limit = input('get.limit');
            $ret = $this->assembleData($page,$limit);
            return $ret;
        }
        return $this->fetch();
    }


    private function assembleData($page,$limit){
        $where = input('get.where')?[['title','like','%'.trim(input('get.where')).'%']]:'';
        $data = AssembleList::where($where)->page($page,$limit)->select()->each(function ($item){
            $good = GoodsList::where('id',$item->good_id)->find();
            $item->good_name  = $good['good_name'];
            $item->image      = $good['image'];
            $item->price      = $good['price'];
        })->toArray();
        $count = AssembleList::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function assembleStatus(){
        $post = input('post.');
        AssembleList::update([$post['name']=>$post['value']],['id'=>$post['id']]);
        return 1;
    }

    public function assembleAdd(){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Assemble.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            list($start_time,$end_time)= explode('至',$post['time'],2);
            $data = [
                'good_id'=>$post['good_id'],
                'start_time'=>strtotime(trim($start_time)),
                'end_time'=>strtotime(trim($end_time)),
                'number'=>$post['number'],
                'hours'=>$post['hours'],
                'a_price'=>$post['a_price'],
                'analog'=>$post['analog'],
                'status'=>$post['status']
            ];
            $ret = AssembleList::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $goods = GoodsList::where('status',1)->field('id,good_name,price')->select()->toArray();
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    public function assembleDel(){
        $id = input('post.id');
        $ret = AssembleList::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>Env::get('msg.del_error')]);
        }
        return json(['code'=>1,'msg'=>Env::get('msg.del_success')]);
    }


    public function assembleEdit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Assemble.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            list($start_time,$end_time)= explode('至',$post['time'],2);
            $data = [
                'good_id'=>$post['good_id'],
                'start_time'=>strtotime(trim($start_time)),
                'end_time'=>strtotime(trim($end_time)),
                'number'=>$post['number'],
                'hours'=>$post['hours'],
                'a_price'=>$post['a_price'],
                'analog'=>$post['analog'],
                'status'=>$post['status']
            ];
            $ret = AssembleList::update($data,['id'=>$id]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $goods = GoodsList::where('status',1)->field('id,good_name,price')->select()->toArray();
        $data = AssembleList::where('id',$id)->find();
        $data['time'] = $data['start_time']." 至 ".$data['end_time'];
        $price = GoodsList::where('id',$data['good_id'])->value('price');
        $this->assign('goods',$goods);
        $this->assign('data',$data);
        $this->assign('price',$price);
        return $this->fetch();
    }



















}